package com.bzyd.verify.utils;

import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.concurrent.TimeUnit;

public class RedisUtil {
    private static StringRedisTemplate redisTemplate;

    public static void setRedisTemplate(StringRedisTemplate stringRedisTemplate){
        RedisUtil.redisTemplate = stringRedisTemplate;
    }

    public static void set(String key, String value) {
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        operations.set(key,value);
    }

    public static void set(String key, String value, int timeout) {
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        operations.set(key,value,timeout, TimeUnit.SECONDS);
    }

    public static String get(String key) {
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        return operations.get(key);
    }

    public static void del(String key) {
        redisTemplate.delete(key);
    }

    public static Long incr(String key) {
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        Long increment = operations.increment(key,1);
        return increment;
    }

    public static Long decr(String key) {
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        Long increment = operations.increment(key,-1);
        return increment;
    }

    public static void setExp(String key, int seconds) {
        redisTemplate.expire(key,seconds,TimeUnit.SECONDS);
    }

    public static Long getExp(String key) {
        Long expire = redisTemplate.getExpire(key);
        return expire;
    }

    public static void lpush(String key, Object value) {
        ListOperations list = redisTemplate.opsForList();
        list.leftPush(key,value);
    }

    public static Object rpop(String key) {
        ListOperations opsForList = redisTemplate.opsForList();
        return opsForList.rightPop(key);
    }

    public static void hset(String key, String item, String value) {
        HashOperations<String, Object, Object> opsForHash = redisTemplate.opsForHash();
        opsForHash.put(key,item,value);
    }

    public static String hget(String key, String item) {
        HashOperations opsForHash = redisTemplate.opsForHash();
        return (String) opsForHash.get(key,item);
    }

    public static void hdel(String key, String item) {
        HashOperations opsForHash = redisTemplate.opsForHash();
        opsForHash.delete(key,item);
    }

}
